#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    typedef pair<int, int> PII;
    int dx[4] = { 1,-1,0,0 };
    int dy[4] = { 0,0,-1,1 };
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        int oldcolor = image[sr][sc];
        if (color == oldcolor) return image;
        queue<PII> q;
        q.push({ sr,sc });
        int m = image.size();
        int n = image[0].size();
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            image[a][b] = color;
            for (int i = 0;i < 4;i++)
            {
                int x = a + dx[i];int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == oldcolor)
                {
                    q.push({ x,y });
                }
            }
        }
        return image;
    }
};
